﻿import QtQuick 2.0

Item {
    id: roleRoot;
    property alias name: nameText.text;
    property rect roleRect: Qt.rect(x,roleItem.y,roleItem.width,roleItem.height);
    
    width: 66;
    height: column.height;
    
    function setPosition(x,y){
        obj.targetX = x-width/2;
        obj.targetY = y - roleItem.height/2 - roleItem.y;
        startWalkTask();
    }
    
    function startWalkTask(){
        var maxV = Math.max(Math.abs(x-obj.targetX),Math.abs(y-obj.targetY));
        var dur = maxV / obj.speedDis * obj.speedDur;
        xAni.duration = dur;
        yAni.duration = dur;
        
        xAni.restart();
        yAni.restart();
    }
    
    QtObject{
        id: obj;
        property int targetX: 0;
        property int targetY: 0;
        property int speedDis: 10; //每speedDur走10px
        property int speedDur: 45; //每45ms走speed
    }
    
    NumberAnimation {
        id: xAni;
        target: roleRoot
        to: obj.targetX
        property: "x"
        easing.type: Easing.Linear
    }
    
    NumberAnimation {
        id: yAni;
        target: roleRoot
        to: obj.targetY
        property: "y"
        easing.type: Easing.Linear
    }
    
    Column{
        id: column;
        width: parent.width;
        spacing: 8;
        
        Text {
            id: nameText;
            font.pointSize: 8;
            style: Text.Sunken;
            color: "black";
            styleColor: "green";
            anchors.horizontalCenter: parent.horizontalCenter;
        }
        
        Rectangle{
            id: roleItem;
            width: parent.width;
            height: 88;
            color: Qt.rgba((Math.random()*10000)%255/100,(Math.random()*10000)%255/100,(Math.random()*10000)%255/100,0.9);
            radius: 2;
            border.color: "black";
        }
    }
}
